#include "kernel_operator.h"
#include <type_traits>
using namespace AscendC;
constexpr int32_t BUFFER_NUM = 2;
template<typename TYPE_Y> class KernelAsinhGrad {
    using T = TYPE_Y;
public:
    __aicore__ inline KernelAsinhGrad() {}
    __aicore__ inline void Init(GM_ADDR y, GM_ADDR dy, GM_ADDR z, 
                            uint32_t CoreDataNum, uint32_t finalTileNum, uint32_t tileDataNum, uint32_t TailDataNum) {
        ASSERT(GetBlockNum() != 0 && "block dim can not be zero!");
        this->coreDataNum = CoreDataNum;
        this->tileNum = finalTileNum;
        this->tileDataNum = tileDataNum;
        this->tailDataNum = TailDataNum;

        yGm.SetGlobalBuffer((__gm__ DTYPE_Y*)y, this->coreDataNum);
        dyGm.SetGlobalBuffer((__gm__ DTYPE_DY*)dy, this->coreDataNum);
        zGm.SetGlobalBuffer((__gm__ DTYPE_Z*)z, this->coreDataNum);

        pipe.InitBuffer(inQueueY, BUFFER_NUM, this->tileDataNum * sizeof(DTYPE_Y));
        pipe.InitBuffer(inQueueDy, BUFFER_NUM, this->tileDataNum * sizeof(DTYPE_DY));
        pipe.InitBuffer(outQueueZ, BUFFER_NUM, this->tileDataNum * sizeof(DTYPE_Z));
        // if constexpr (std::is_same_v<T, int8_t> || std::is_same_v<T, signed char>) {
            pipe.InitBuffer(QueueTmpZ, this->tileDataNum * sizeof(float));
            // pipe.InitBuffer(QueueTmpDy, this->tileDataNum * sizeof(float));
            pipe.InitBuffer(QueueTmpY, this->tileDataNum * sizeof(float));
        // }
        // else if constexpr (std::is_same_v<T, int32_t>)
        // {
        //     pipe.InitBuffer(QueueTmp1, this->tileDataNum * sizeof(float));
        //     pipe.InitBuffer(QueueTmp2, this->tileDataNum * sizeof(float));
        // }
    }
    __aicore__ inline void Process() {
        int32_t loopCount = this->tileNum;
        this->processDataNum = this->tileDataNum;
        for (int32_t i = 0; i < loopCount; i++) {
            if (i == this->tileNum - 1) {
              this->processDataNum = this->tailDataNum;
            }
            CopyIn(i);
            Compute(i);
            CopyOut(i);
        }
    }

private:
    __aicore__ inline void CopyIn(int32_t progress) {
        LocalTensor<DTYPE_Y> yLocal = inQueueY.AllocTensor<DTYPE_Y>();
        LocalTensor<DTYPE_DY> dyLocal = inQueueDy.AllocTensor<DTYPE_DY>();
        DataCopy(yLocal, yGm[progress * this->tileDataNum], this->processDataNum);
        DataCopy(dyLocal, dyGm[progress * this->tileDataNum], this->processDataNum);
        inQueueY.EnQue(yLocal);
        inQueueDy.EnQue(dyLocal);
    }
    __aicore__ inline void Compute(int32_t progress) {
        LocalTensor<DTYPE_Y> yLocal = inQueueY.DeQue<DTYPE_Y>();
        LocalTensor<DTYPE_DY> dyLocal = inQueueDy.DeQue<DTYPE_DY>();
        LocalTensor<DTYPE_Z> zLocal = outQueueZ.AllocTensor<DTYPE_Z>();

        if constexpr (std::is_same_v<T, half>) {

            // auto tmpy = QueueTmpY.Get<float>();
            // // auto tmpdy = QueueTmpDy.Get<float>();
            // auto tmpz = QueueTmpZ.Get<float>();
            // Cast(tmpy, yLocal, RoundMode::CAST_NONE, this->processDataNum);
            // Exp(tmpz, tmpy, this->processDataNum);
            // Add(dyLocal, dyLocal, dyLocal, this->processDataNum);
            // Cast(tmpy, dyLocal, RoundMode::CAST_NONE, this->processDataNum);
            // Mul(tmpy, tmpz, tmpy, this->processDataNum);
            // Mul(tmpz, tmpz, tmpz, this->processDataNum);
            // Adds(tmpz, tmpz, (float)(1), this->processDataNum);
            // Div(tmpz, tmpy, tmpz, this->processDataNum);
            // Cast(zLocal, tmpz, RoundMode::CAST_NONE, this->processDataNum);


            // auto tmpy = QueueTmpY.Get<float>();
            // // auto tmpdy = QueueTmpDy.Get<float>();
            // auto tmpz = QueueTmpZ.Get<float>();
            // Cast(tmpy, yLocal, RoundMode::CAST_NONE, this->processDataNum);
            // Exp(tmpz, tmpy, this->processDataNum);
            // Muls(tmpy, tmpy, (float)-1, this->processDataNum);
            // Exp(tmpy, tmpy, this->processDataNum);
            // Add(tmpz, tmpy, tmpz, this->processDataNum);

            // Add(dyLocal, dyLocal, dyLocal, this->processDataNum);
            // Cast(tmpy, dyLocal, RoundMode::CAST_NONE, this->processDataNum);
            // Div(tmpz, tmpy, tmpz, this->processDataNum);
            // Cast(zLocal, tmpz, RoundMode::CAST_NONE, this->processDataNum);

            Exp(zLocal, yLocal, this->processDataNum);
            Mul(yLocal, zLocal, zLocal, this->processDataNum);
            Adds(yLocal, yLocal, (DTYPE_Y)(1), this->processDataNum);
            // Div(zLocal, yLocal, zLocal, this->processDataNum);
            Mul(dyLocal, dyLocal, zLocal, this->processDataNum);
            Add(dyLocal, dyLocal, dyLocal, this->processDataNum);
            Div(zLocal, dyLocal, yLocal, this->processDataNum);


            // auto tmpy = QueueTmpY.Get<float>();
            // auto tmpdy = QueueTmpDy.Get<float>();
            // auto tmpz = QueueTmpZ.Get<float>();
            // Cast(tmpy, yLocal, RoundMode::CAST_NONE, this->processDataNum);
            // Cast(tmpdy, dyLocal, RoundMode::CAST_NONE, this->processDataNum);




            // auto tmpy = QueueTmpY.Get<float>();
            // // auto tmpdy = QueueTmpDy.Get<float>();
            // auto tmpz = QueueTmpZ.Get<float>();
            // Cast(tmpy, yLocal, RoundMode::CAST_NONE, this->processDataNum);
            // Cast(tmpz, dyLocal, RoundMode::CAST_NONE, this->processDataNum);

            // // Duplicate(tmpz, (float)(0), this->processDataNum);
            // // Sub(tmpz, tmpz, tmpy, this->processDataNum);
            // Abs(tmpy, tmpy, this->processDataNum);
            // Exp(tmpy, tmpy, this->processDataNum);
            // // Exp(tmpz, tmpz, this->processDataNum);
            // // Add(tmpz, tmpz, tmpy, this->processDataNum);

            // Add(tmpz, tmpz, tmpz, this->processDataNum);
            // Div(tmpz, tmpz, tmpy, this->processDataNum);
            // Cast(zLocal, tmpz, RoundMode::CAST_NONE, this->processDataNum);

            // Exp(tmpz, tmpy, this->processDataNum);
            // Mul(tmpy, tmpz, tmpz, this->processDataNum);
            // Adds(tmpy, tmpy, (float)(1), this->processDataNum);


            // Div(tmpz, tmpy, tmpz, this->processDataNum);

            // Cast(tmpy, dyLocal, RoundMode::CAST_NONE, this->processDataNum);

            // Add(tmpy, tmpy, tmpy, this->processDataNum);
            // Div(tmpz, tmpy, tmpz, this->processDataNum);

            // Cast(zLocal, tmpz, RoundMode::CAST_NONE, this->processDataNum);

            // Duplicate(zLocal, (DTYPE_Y)(0), this->processDataNum);
            // Sub(zLocal, zLocal, yLocal, this->processDataNum);
            // Exp(yLocal, yLocal, this->processDataNum);
            // Exp(zLocal, zLocal, this->processDataNum);
            // Add(zLocal, zLocal, yLocal, this->processDataNum);
            // Add(dyLocal, dyLocal, dyLocal, this->processDataNum);
            // Div(zLocal, dyLocal, zLocal, this->processDataNum);

            // Exp(zLocal, yLocal, this->processDataNum);
            // Mul(yLocal, zLocal, zLocal, this->processDataNum);
            // Adds(yLocal, yLocal, (DTYPE_Y)(1), this->processDataNum);
            // Div(zLocal, yLocal, zLocal, this->processDataNum);
            
            // Add(dyLocal, dyLocal, dyLocal, this->processDataNum);
            // Div(zLocal, dyLocal, zLocal, this->processDataNum);
        }
        else {
            // auto tmpy = QueueTmpY.Get<float>();
            // // auto tmpdy = QueueTmpDy.Get<float>();
            // auto tmpz = QueueTmpZ.Get<float>();
            // Cast(tmpy, yLocal, RoundMode::CAST_NONE, this->processDataNum);
            // // Cast(tmpdy, dyLocal, RoundMode::CAST_NONE, this->processDataNum);

            // // Duplicate(tmpz, (float)(0), this->processDataNum);
            // // Sub(tmpz, tmpz, tmpy, this->processDataNum);
            // // Exp(tmpy, tmpy, this->processDataNum);
            // // Exp(tmpz, tmpz, this->processDataNum);
            // // Add(tmpz, tmpz, tmpy, this->processDataNum);

            // Exp(tmpz, tmpy, this->processDataNum);
            // Mul(tmpy, tmpz, tmpz, this->processDataNum);
            // Adds(tmpy, tmpy, (float)(1), this->processDataNum);
            // Div(tmpz, tmpy, tmpz, this->processDataNum);

            // Cast(tmpy, dyLocal, RoundMode::CAST_NONE, this->processDataNum);
            // Add(tmpy, tmpy, tmpy, this->processDataNum);
            // Div(tmpz, tmpy, tmpz, this->processDataNum);

            // Cast(zLocal, tmpz, RoundMode::CAST_NONE, this->processDataNum);

            Exp(zLocal, yLocal, this->processDataNum);
            Mul(yLocal, zLocal, zLocal, this->processDataNum);
            Adds(yLocal, yLocal, (DTYPE_Y)(1), this->processDataNum);
            Div(zLocal, yLocal, zLocal, this->processDataNum);
            
            Add(dyLocal, dyLocal, dyLocal, this->processDataNum);
            Div(zLocal, dyLocal, zLocal, this->processDataNum);
        }
        // if constexpr (std::is_same_v<T, float>) {
        //     auto tmpbit = QueueTmpbit.Get<uint8_t>();
        //     auto tmp1 = QueueTmp1.Get<DTYPE_X>();
        //     Mul(yLocal, xLocal, xLocal, this->processDataNum);
        //     Adds(yLocal, yLocal, (DTYPE_X)1, this->processDataNum);
        //     Sqrt(yLocal, yLocal, this->processDataNum);
        //     Abs(tmp1, xLocal, this->processDataNum);
        //     Add(yLocal, yLocal, tmp1, this->processDataNum);
        //     Ln(yLocal, yLocal, this->processDataNum);
        //     CompareScalar(tmpbit, xLocal, (DTYPE_X)0, AscendC::CMPMODE::LT, this->processDataNum);
        //     Duplicate(tmp1, (DTYPE_X)(-1), this->processDataNum);
        //     Select(xLocal, tmpbit, tmp1, (DTYPE_X)(1), SELMODE::VSEL_TENSOR_SCALAR_MODE, this->processDataNum);
        //     Mul(yLocal, xLocal, yLocal, this->processDataNum);
        // }
        // else
        // {
        //     auto tmp1 = QueueTmp1.Get<float>();
        //     auto tmpbit = QueueTmpbit.Get<uint8_t>();
        //     auto tmpx = QueueTmpX.Get<float>();
        //     auto tmpy = QueueTmpY.Get<float>();
        //     Cast(tmpx, xLocal, RoundMode::CAST_NONE, this->processDataNum);
            
        //     Mul(tmpy, tmpx, tmpx, this->processDataNum);
        //     Adds(tmpy, tmpy, (float)1, this->processDataNum);
        //     Sqrt(tmpy, tmpy, this->processDataNum);
        //     Abs(tmp1, tmpx, this->processDataNum);
        //     Add(tmpy, tmpy, tmp1, this->processDataNum);
        //     Ln(tmpy, tmpy, this->processDataNum);
        //     CompareScalar(tmpbit, tmpx, (float)0, AscendC::CMPMODE::LT, this->processDataNum);
        //     Duplicate(tmp1, (float)(-1), this->processDataNum);
        //     Select(tmpx, tmpbit, tmp1, (float)(1), SELMODE::VSEL_TENSOR_SCALAR_MODE, this->processDataNum);
        //     Mul(tmpy, tmpx, tmpy, this->processDataNum);

        //     Cast(yLocal, tmpy, RoundMode::CAST_NONE, this->processDataNum);
        // }
        
        inQueueY.FreeTensor(yLocal);
        inQueueDy.FreeTensor(dyLocal);
        outQueueZ.EnQue<DTYPE_Z>(zLocal);
    }
    __aicore__ inline void CopyOut(int32_t progress) {
        LocalTensor<DTYPE_Z> zLocal = outQueueZ.DeQue<DTYPE_Z>();
        DataCopy(zGm[progress * this->tileDataNum], zLocal, this->processDataNum);
        outQueueZ.FreeTensor(zLocal);
    }

private:
    TPipe pipe;
    TQue<QuePosition::VECIN, BUFFER_NUM> inQueueY,inQueueDy;
    TQue<QuePosition::VECOUT, BUFFER_NUM> outQueueZ;
    TBuf<QuePosition::VECCALC> QueueTmpDy, QueueTmpY, QueueTmpZ;

    GlobalTensor<DTYPE_Y> yGm;
    GlobalTensor<DTYPE_DY> dyGm;
    GlobalTensor<DTYPE_Z> zGm;
    uint32_t coreDataNum;
    uint32_t tileNum;
    uint32_t tileDataNum;
    uint32_t tailDataNum;
    uint32_t processDataNum;
};

extern "C" __global__ __aicore__ void asinh_grad(GM_ADDR y, GM_ADDR dy, GM_ADDR z, GM_ADDR workspace, GM_ADDR tiling) {
    GET_TILING_DATA(tiling_data, tiling);
    // TODO: user kernel impl
    KernelAsinhGrad<DTYPE_Y> op;
    op.Init(y, dy, z,
            tiling_data.CoreDataNum, tiling_data.finalTileNum, tiling_data.tileDataNum, tiling_data.TailDataNum);
    op.Process();
}